<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>广告修改</title>
	<link rel="stylesheet" href="../../../component/pear/css/pear.css" />
</head>
<body>
<form class="layui-form" action="">
	<div class="mainBox">
		<div class="main-container">
			<div class="layui-form-item">
				<label class="layui-form-label">资源地址</label>
				<div class="layui-input-block">
					<input type="text"   name="attachuri" lay-verify="idcardpic" autocomplete="off" placeholder="照片" id="attachuri"
						   class="layui-input">
					<input class="btn-upload file-up" style="display: inline-block;" type="file"   name="edit" id="edit" value="上传">
				</div>
				<div class="layui-input-block">
					<img id="preview" height="200px" width="300px" src="#" alt="没有选择"/>
					<span id="demoText"></span>
				</div>
			</div>
			<!--					<div class="layui-form-item layui-inline">-->
			<!--						<label class="layui-form-label">发布时间</label>-->
			<!--						<div class="layui-input-inline">-->
			<!--							<input type="date" name="posttime" placeholder="请输入发布时间" class="layui-input">-->
			<!--						</div>-->
			<!--					</div>-->
			<!--					<div class="layui-form-item layui-inline">-->
			<!--						<label class="layui-form-label">发布人</label>-->
			<!--						<div class="layui-input-inline">-->
			<!--							<input type="text" name="poster" placeholder="请输入发布人" class="layui-input">-->
			<!--						</div>-->
			<!--					</div>-->
			<div class="layui-form-item layui-inline">
				<label class="layui-form-label">轮播板块</label>
				<div class="layui-input-inline">
					<select name="carousel" lay-verify="carousel" lay-search>
						<option value=""></option>
						<option value="Media_coverage">媒体报道</option>
						<option value="Cooperation">交流合作</option>
						<option value="Contact_Us">联系我们</option>
						<option value="Service_group">服务群体</option>
						<option value="Expert_team">专家团队</option>
						<option value="Course_Introduction">课程介绍</option>
						<option value="Company_News">公司新闻</option>
						<option value="Activity_Lectures">活动讲座</option>
						<option value="Speech_Science">言语科普</option>
						<option value="Media_Video">媒体视频</option>
						<option value="Corporate_activities">企业活动</option>
						<option value="Brand_promotion">品牌宣传</option>
						<option value="House_training">家庭训练技巧</option>
						<option value="successful_case">成功案例</option>
						<option value="SPEECH">朗朗SPEECH</option>
<!--						<option value="STAR_CLASS">STAR课程</option>-->
					</select>
				</div>
			</div>
			<div class="layui-form-item layui-inline">
				<label class="layui-form-label">是否显示</label>
				<div class="layui-input-inline">
					<input type="radio" name="isvalid" value="true" title="显示" checked>
					<input type="radio" name="isvalid" value="false" title="不显示" >
				</div>
			</div>
			<div class="layui-form-item layui-inline">
				<label class="layui-form-label">资源类型</label>
				<div class="layui-input-inline">
					<input type="text" name="restype" placeholder="请输入资源类型" class="layui-input">
				</div>
			</div>
		</div>
	</div>
	<div class="bottom">
		<div class="button-container">
			<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
					lay-filter="user-save">
				<i class="layui-icon layui-icon-ok"></i>
				提交
			</button>
			<button type="reset" class="pear-btn pear-btn-sm">
				<i class="layui-icon layui-icon-refresh"></i>
				重置
			</button>
		</div>
	</div>
</form>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script>
	layui.use(['form', 'jquery','upload'], function() {
		let form = layui.form;
		let $ = layui.jquery;
		var upload = layui.upload;
		$.ajaxSetup({
			headers:{
				'Authorization':window.sessionStorage.getItem("token")
			}
		})
		$(function (){
			$.getJSON(window.BASEPATH+"attach/select/"+sessionStorage.getItem("id"),
					function(res){
						if(res.code!=0)
							return layer.msg('信息检索失败');
						let us=res.data;
						// $("[name='attachuri']").val(data.attachuri);
						// $("[name='posttime']").val(data.posttime);
						// $("[name='poster']").val(data.poster);
						// $("[name='carousel']").val(data.carousel);
						// // $("[name='isvalid']").val(data.isvalid);
						// $("[name='isvalid'][value=]").prop("checked", true);
						// $("[name='restype']").val(data.restype);
						for (let k in us) {
							$(`[name=${k}]`).val(us[k]);
							if (k == "isvalid") {
								$(`[name=${k}][value='${us[k]}']`).prop("checked", true);
								continue;
							}
							if (k == "attachuri") {
								$("form [name=" + k + "]").val(us[k]);
								if (us.attachuri.indexOf("http") == 0)
									$("#preview").attr("src", us.attachuri);
								else
									$("#preview").attr("src", window.BASEPATH + us.attachuri);
								continue;
							}
						}
						form.render();
						// $('#preview').attr('src', url2+data.imgurl);
						// alert("res.data.isvalid:"+res.data.isvalid)
						// $('[name="isvalid"][value='+res.data.isvalid+']').prop("checked", true);
						// $('[name="sortorder"][value='+res.data.sortorder+']').prop("checked", true);
						// $('[name="target"][value='+res.data.target+']').prop("checked", true);
					});
		})
		// $(function (){
		// 	$.getJSON(window.BASEPATH+"attach/select/"+sessionStorage.getItem("id"),
		// 			function(res){
		// 				if(res.code!=0)
		// 					return layer.msg('信息检索失败');
		// 				let adv=res.data;
		// 				console.log(adv);
		// 				console.log(adv.carousel);
		// 				for (let k in adv) {
		// 					// if (k == "attachuri") {
		// 					// 	if (adv.attachuri.indexOf("http") == 0)
		// 					// 		$("#preview").attr("src", adv.attachuri);
		// 					// 	else
		// 					// 		$("#preview").attr("src", window.BASEPATH + adv.attachuri);
		// 					// 	continue;
		// 					// }
		// 					if (k == "attachuri") {
		// 						$("form [name=" + k + "]").val(adv[k]);
		// 						if (adv.attachuri.indexOf("http") == 0)
		// 							$("#preview").attr("src", adv.attachuri);
		// 						else
		// 							$("#preview").attr("src", window.BASEPATH + adv.attachuri);
		// 						continue;
		// 					}
		// 					if (k == "isvalid") {
		// 						$(`[name=${k}][value='${adv[k]}']`).prop("checked", true);
		// 						continue;
		// 					}
		// 					if(k=="carousel"){
		// 						$(`[name=${k}][value='${adv[k]}']`).prop("checked", true);
		// 						continue;
		// 					}
		// 					$(`[name=${k}]`).val(adv[k]);
		// 				}
		// 				form.render();
		// 			});
		// })
		form.on('submit(user-save)', function(data) {
			data.field.id=sessionStorage.getItem("id");
			console.log(data.field);
			$.ajax({
				url: window.BASEPATH+'attach/edit',
				data:data.field,
				type: 'post',
				success: function(result) {
					if (result.code==0) {
						layer.msg(result.msg,{
							icon: 1,
							time: 1000
						}, function() {
							parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
							parent.layui.table.reload("user-table");
						});
					} else {
						layer.msg(result.msg, {
							icon: 2,
							time: 1000
						});
					}
				}
			})
			return false;
		});
		var uploadInst = upload.render({
			elem: '#edit'
			,url: window.BASEPATH+'upload2' //改成您自己的上传接口
			,accept: 'file'
			//,accept: '*'
			,auto: true  //选择文件后不自动上传

			,choose: function(obj){
				//预读本地文件示例，不支持ie8
				obj.preview(function(index, file, result){
					$('#preview').attr('src', result); //图片链接（base64）
				});
			}
			,before: function(obj){
				layer.msg('上传中', {icon: 16, time: 0});
			}
			,done: function(res){
				//如果上传失败
				if(res.code > 0){
					return layer.msg('上传失败');
				}
				$("#idcardpic").val(res.data);
				//上传成功的一些操作
				layer.msg("信息上传成功！",{time: 3000});
			}
			,error: function(){
				//演示失败状态，并实现重传
				var demoText = $('#demoText');
				demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
				demoText.find('.demo-reload').on('click', function(){
					uploadInst.upload();
				});
			}

		});
	})
</script>
<script>
</script>
</body>
</html>
